package org.vectrics.recruiter.reporting;



import java.awt.Color;
import java.util.Collection;
import java.util.Iterator;
import java.util.Locale;
import java.util.TreeSet;

import org.apache.log4j.Logger;
import org.vectrics.VectricsException;
import org.vectrics.company.Company;
import org.vectrics.company.CompanyNameThenDivisionComparator;
import org.vectrics.company.CompanySelectCriteria;
import org.vectrics.domain.Domain;
import org.vectrics.provider.CoreServiceLocator;
import org.vectrics.recruiter.job.Job;

import com.lowagie.text.Document;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.BaseFont;


public class JobListReport extends RecruitBaseReport {
	private transient static Logger log = Logger.getLogger(JobListReport.class);
    
	
    private String jobId = null;
	private Job job = null;
	private boolean groupByCompany = false;
    private boolean activeOnly = true;
    private int FOOTER_FONT_SIZE = 8;
	private BaseFont FOOTER_FONT = null;
	private Color FOOTER_FONT_COLOR = new Color(100, 100, 150);
	private String companyId = null;
	
	
	public JobListReport(Domain domain, Locale locale) {
		super(domain, locale);
		try {
			FOOTER_FONT = 
				BaseFont.createFont(BaseFont.HELVETICA_BOLD, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
		}
		catch (Exception de) {
			log.error("Exception creating footer font", de);
		}
	}
	

	protected Rectangle getPageSize() {
		return(PageSize.A4.rotate());
	}


	 
	protected void fill(Document document) throws VectricsException {
		log.debug("Job List Report - building");
		try {
			
			if (this.getActiveOnly()) {
				addCodedParagraph("phrase.jobs.active", ReportConstants.titleFont);
			}
			else {
				addCodedParagraph("phrase.jobs.all", ReportConstants.titleFont);
			}
			if (groupByCompany) {
				Collection companyList = findSortedAllCompanyList();
				Iterator iter = companyList.iterator();
				while (iter.hasNext()) {
					//String id = (String)iter.next();
					Company company = (Company)iter.next();
					
					document.add(new Paragraph(" ", ReportConstants.titleFont));
					
					
					JobListPdfTable jobTable = new JobListPdfTable(this.getDomain(), this.getLocale());
					jobTable.setCompany(company);
					jobTable.setActiveOnly(this.getActiveOnly());
					jobTable.fill();
					document.add(jobTable);
				}
			} else {
				JobListPdfTable jobTable = new JobListPdfTable(this.getDomain(), this.getLocale());
				jobTable.setCompany(null);
				jobTable.setActiveOnly(this.getActiveOnly());
				jobTable.fill();
				document.add(jobTable);
			}

		}
		catch(Exception e) {
			log.error("Exception creating pdf document - in fill()", e);
			throw new VectricsException("Exception creating pdf document - fill()", e);
		}
        
	}

	protected Collection findSortedAllCompanyList() throws VectricsException {
		TreeSet companySortedSet = new TreeSet(new CompanyNameThenDivisionComparator());
		Collection companies = CoreServiceLocator.getCompanyService().findCompanies(new CompanySelectCriteria(), 0, 100);
		Iterator iter = companies.iterator();
		while (iter.hasNext()) {
			Company company = (Company)iter.next();
			companySortedSet.add(company);
		}
		return(companySortedSet);
	}

    
	protected void setupHeaderFooter(Document document) throws VectricsException {
		//
	}


	

	public boolean getActiveOnly() {
		return activeOnly;
	}


	public void setActiveOnly(boolean activeOnly) {
		this.activeOnly = activeOnly;
	}

	/**
	 * @return
	 */
	public String getCompanyId() {
		
		return companyId;
	}

	/**
	 * @param companyId
	 */
	public void setCompanyId(String companyId) {
		this.companyId = companyId;
	}

	/**
	 * @return Returns the groupByCompany.
	 */
	public boolean getGroupByCompany() {
		return groupByCompany;
	}

	/**
	 * @param groupByCompany The groupByCompany to set.
	 */
	public void setGroupByCompany(boolean groupByCompany) {
		this.groupByCompany = groupByCompany;
	}

}
